
package com.hd.determine.modules.alarm.determine.judges;

import com.hd.common.constant.BizConstant;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.DistanceUtils;
import com.hd.common.utils.ValueUtils;
import com.hd.determine.core.utils.StaticPropUtil;
import com.hd.determine.modules.alarm.Determiner;
import com.hd.determine.modules.alarm.model.AlarmRule;
import com.hd.determine.modules.vehicle.model.VehicleEndPos;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title OffGpsDeterminer.java
 * @Description 不定位研判者
 * @Package com.hd.sup.modules.alarm.determine.judges
 * @date 2021/1/29 11:26 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
public class OffGpsDeterminer extends Determiner<VehicleEndPos> {

    public OffGpsDeterminer(VehicleEndPos data, AlarmRule alarmRule) {
        super(data, alarmRule);
    }

    @Override
    public void startDetermine() {
        String platNo = data.getPlatNo();
        String platNoColor = data.getPlatNoColor();
        Date lastGpsTime = data.getLastGpsTime();
        Double lastLng = data.getLastLng();
        Double lastLat = data.getLastLat();
        Date currentGpsTime = data.getGpsTime();
        Double currentLng = data.getLng();
        Double currentLat = data.getLat();

        //研判因子不存在则忽略
        if (lastGpsTime == null || lastLng == null || lastLat == null ||
                currentGpsTime == null || currentLng == null || currentLat == null) {
            return;
        }

        double offsetDistance = DistanceUtils.getDistance(lastLng, lastLat, currentLng, currentLat);
        long overGpsTime = ValueUtils.parseLong(DateUtil.getDiffMins(currentGpsTime, lastGpsTime));
        boolean isOffsetExceeded = offsetDistance > alarmRule.getExceededDistance();
        boolean isOverGpsTime = overGpsTime > alarmRule.getExceededMinute();

        if(StaticPropUtil.SHOW_ALARM_LOG) {
            log.info("[alarmDeterminer] 即将开始研判 车牌:{} 颜色: {}  不定位报警,研判因子:\n lastGpsTime:{} \n lastLng: {} \n lastLat:{} \n currentGpsTime: {} \n currentLng:{} \n currentLat:{} \n offsetDistance: {} \n overGpsTime:{}\n",
                    platNo,
                    platNoColor,
                    lastGpsTime,
                    lastLng,
                    lastLat,
                    currentGpsTime,
                    currentLng,
                    currentLat,
                    offsetDistance,
                    overGpsTime);
        }
        if (isOffsetExceeded && isOverGpsTime) {
            //GPS超时 + 偏移溢出 => 产生不定位报警
            data.setBizAlarmType(BizConstant.AlarmType.OFF_GPS.key());
            if(StaticPropUtil.SHOW_ALARM_LOG) {
                log.info("[rabbitmq] 根据研判规则:{} 车辆：{} {}判定为 {} 报警", alarmRule.getRuleName(), data.getPlatNo(), data.getPlatNoColor(), alarmRule.getAlarmName());
            }
            needProduce = true;
        }
    }
}
